import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/pages/Home.vue'
import About from '@/pages/About.vue'
import News from '~/pages/News.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    component: About
  },
  {
    path: '/news',
    name: 'News',
    component: News,
    beforeEnter(to, from ,next) {
      console.log('路由独享的守卫') // localStorage cookie 不生效，因为走服务端和客户端
      next()
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  routes
})

// 全局前置守卫
router.beforeEach((to, from ,next) => {
  console.log(to) // 服务端客户端都会打印 
  if ( to.name === 'Home') {
    next('/news')
  } else {
    next()
  }
})

export function createRouter() {
  return router
}

// const router = new VueRouter({
//   mode: 'history',
//   base: process.env.BASE_URL,
//   routes
// })

// export default router
